$(document).ready(function(){
	$('#delete').click(function(){
		var data = new Array();
		$("input[name='del']:checked").each(function(i) {
        data.push($(this).val());
		});
		
		//var datastring = $('#formmail').serialize();
		$.ajax({
			type: 'POST',
			url: 'delete.php',
			data: 'id='+data,
			success:function(html){
				if(html=='deleted'){
					alert("Success ! Deleted");
					window.location.href="index.php";
					
				}
				else if(html=="nondelete"){
					alert('Can not delete!');
				}
			}
		});
	});
	
	$('.edit').each(function(){
		$(this).click(function(){
			var edit_id = $(this).attr('id').replace('edit_','');
			var datastringsel = 'id='+edit_id;
			$.ajax({
				type: 'POST',
				url: 'select.php',
				data: datastringsel,
				success:function(html){
					if(html=='nonselect'){
						$('#notesedit').css('color','red');
						$('#notesedit').append('<em>Sorry !System can not edit</em>');
					}
					else{
						$('#edittext_lightbox').val(html);
					}
				}
			});
			
			$('.backdrop, .box').animate({'opacity':'.50'}, 300, 'linear');
			$('.box').animate({'opacity':'1.00'}, 300, 'linear');
			$('.backdrop, .box').css('display', 'block');
			
			var edit_text;
			$("#edittext_lightbox").keyup(function () {
			  edit_text = $(this).val();
			}).keyup();

			//Check email
			//var email = $('#email').val();
	
			$('#editbt_lightbox').click(function(){
				var datastring = 'id='+edit_id+'&email='+edit_text;
				var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

				$('#notesedit').empty();
				if (!filter.test(edit_text)) {
					$('#notesedit').css('color','red');
					$('#notesedit').append('<em>Please type valid email!</em>');
					$('#notesedit').focus();
				}
				else{
					$.ajax({
						type: 'POST',
						url: 'edit.php',
						data: datastring,
						success:function(html){
							if(html=='edited'){
								$('#notesedit').css('color','blue');
								$('#notesedit').append('<em>Success! You edited</em>');
								$('.backdrop, .box').animate({'opacity':'0'}, 2000, 'linear', function(){
									window.location.href="index.php";
								});
							}
							else if(html=='nonedit'){
								$('#notesedit').css('color','red');
								$('#notesedit').append('<em>Sorry !System can not edit</em>');
							}
						}
					});
				}
			});
			
			$('#cancelbt_lightbox').click(function(){
				close_box();
			});
			
		});
	});

	$('#insert').click(function(){
		var email = $('#email').val();
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

			$('#notes').empty();
			if (!filter.test(email)) {
				$('#notes').css('color','red');
				$('#notes').append('<em>Please type valid email!</em>');
				$('#email').focus();
			}
			else
			{
				$.ajax({
					type: "POST",
					url: "insert.php",
					data: "email="+email,
					success: function(html){
						if(html=="done"){
							$('#notes').css('color','blue');
							$('#notes').append("<em>Insert is Success!</em>");
						}
						else if(html=="notdone"){
							$('#notes').css('color','red');
							$('#notes').append("<em>Sorry !Insert is not Success!\nMay be similar to some other email<em>");
						}
					}
				});
			}
	});
	
	$('.close').click(function(){
		close_box();
	});

	$('.backdrop').click(function(){
		close_box();
	});
});

function close_box(){
	$('.backdrop, .box').animate({'opacity':'0'}, 300, 'linear', function(){
		$('.backdrop, .box').css('display', 'none');
	});
}